package com.project.controller;

import com.project.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




@RestController
@RequestMapping("/adapterApi")
@Api(tags = "万能请求接口")
public class AdapterApi {


    // 数据库连接信息
    String url = "jdbc:mysql://39.101.64.201:3308/mysql";
    String username = "root";
    String password = "12345679abcABC";
    @GetMapping("/selectById")
    @ApiOperation("根据id查找数据")
    public Result<Map<String, String>> selectById(
            @RequestParam String url,
            @RequestParam String userName,
            @RequestParam String password,
            @RequestParam String dbName,
            @RequestParam String dataId,
            @RequestParam String tableName
            ) {

        Map<String, String> data = new HashMap<>();
        try (Connection conn = DriverManager.getConnection(url, userName, password);
             Statement stmt = conn.createStatement()) {
            //连接指定的数据库
            String str="use "+dbName+";";
            stmt.executeUpdate(str);


            String sql = "SELECT * FROM " + tableName + " WHERE id = " + dataId;
            ResultSet rs = stmt.executeQuery(sql);

            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();

            // Process each row in the result set
            if (rs.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnName(i);
                    String columnValue = rs.getString(i);

                    // 明确处理null值
                    if (columnValue == null) {
                        columnValue = "null";  // 或者使用其他占位符
                    }

                    data.put(columnName, columnValue);
                }
                return Result.success(data);
            } else {
                return Result.error("这个id： " + dataId+"的数据不存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return Result.error("数据库连接错误 " + e.getMessage());
        }
    }


    @GetMapping("/selectAll")
    @ApiOperation("查找所有数据")
    public Result< List<Map<String, String>>> selectAll(
            @RequestParam String url,
            @RequestParam String userName,
            @RequestParam String password,
            @RequestParam String dbName,
            @RequestParam String tableName
    ) {

        Map<String, String> data = new HashMap<>();
        try (Connection conn = DriverManager.getConnection(url, userName, password);
             Statement stmt = conn.createStatement()) {
            //连接指定的数据库
            String str="use "+dbName+";";
            stmt.executeUpdate(str);


            String sql = "SELECT * FROM " + tableName ;
            ResultSet rs = stmt.executeQuery(sql);

            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();

            List<Map<String, String>> list = new ArrayList<>();
            while (rs.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnName(i);
                    String columnValue = rs.getString(i);

                    // 明确处理null值
                    if (columnValue == null) {
                        columnValue = "null";  // 或者使用其他占位符
                    }

                    data.put(columnName, columnValue);

                }

                list.add(data);
            }
            return Result.success(list);

        } catch (SQLException e) {
            e.printStackTrace();
            return Result.error("数据库连接错误 " + e.getMessage());
        }
    }
}
